package pojo;

import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisDemo06 {
    public static void main(String[] args) throws IOException {
        User user = new User();
        user.setId(5);
        user.setUsername("xiaohe");
        user.setFullname("和镇压");
        user.setPassword("66777");
        user.setAddress("山东省青岛市市南");


//1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(is);
//2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行sql
// 参数是一个字符串，该字符串必须是映射配置文件的namespace.id
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.updateOne(user);
       sqlSession.commit();
//4. 释放资源
        sqlSession.close();

    }
}

